/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.foxline.manager.create;

import br.com.foxline.model.DadosIptu;
import br.com.foxline.model.Imovel;
import br.com.foxline.model.Iptu;
import br.com.foxline.service.DadosIptuService;
import br.com.foxline.service.ImovelService;
import br.com.foxline.service.IptuService;
import br.com.foxline.service.ReportService;
import br.com.foxline.util.Msg;
import java.io.IOException;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

/**
 *
 * @author luciano
 */
@ManagedBean
@ViewScoped
public class ManagerIPTU implements Serializable {

    @EJB
    private ImovelService imovelService;
    @EJB
    private IptuService iptuService;
    @EJB
    private DadosIptuService dadosIptuService;
    
    private DadosIptu dadosIptu;

    @PostConstruct
    public void init(){
        Calendar calendar = GregorianCalendar.getInstance();
        int ano = calendar.get(Calendar.YEAR);
        
        dadosIptu = new DadosIptu();
        dadosIptu.setAno(ano);
    }
    
    public void gerarIPTU() {
    //    List<Imovel> imoveis = imovelService.findAll();

      //  for (Imovel imovel : imoveis) {
        Imovel imovel = imovelService.find(41L);
            Iptu iptu = new Iptu();
            iptu.setImovel(imovel);

            BigDecimal valorIptu = iptuService.iptu(imovel, dadosIptu);
            
            iptu.setTcc(iptuService.tcc(imovel, dadosIptu.getUfmp()));
            iptu.setTcl(iptuService.tcl(imovel, dadosIptu.getUfmp()));
            iptu.setTlp(iptuService.tlp(imovel, dadosIptu.getUfmp()));
            
            
            valorIptu = valorIptu.add(iptu.getTcc());
            valorIptu = valorIptu.add(iptu.getTcl());
            valorIptu = valorIptu.add(iptu.getTlp());
            valorIptu = valorIptu.add(dadosIptu.getUfmp());
            valorIptu = valorIptu.add(dadosIptu.getCorrecao());
            
            iptu.setValor(valorIptu.multiply(new BigDecimal(0.9)));
            iptu.setDataIptu(dadosIptu.getVencimento());
            iptu.setProprietario(imovel.getProprietarios().get(0));
            
           int qtd = iptuService.qtdParcelas(iptu.getValor());
           
           if(qtd > 0){
               iptu.setParcelas(iptuService.gerarParcelas(imovel, valorIptu, qtd, dadosIptu.getVencimento()));
           }
           
           iptuService.save(iptu);
       // }
        dadosIptuService.save(dadosIptu);
        Msg.messagemInfo("Iptu gerado");

    }
    
    public void geraBoleto(){
       iptuService.gerarBoleto();
    }
    
    public List<DadosIptu> all(){
        return dadosIptuService.findAll();
    }
    
    public DadosIptu getDadosIptu() {
        return dadosIptu;
    }

    public void setDadosIptu(DadosIptu dadosIptu) {
        this.dadosIptu = dadosIptu;
    }
    
    
}
